﻿using AutoMapper;
using B.S.Database.Domain.Entity;
using B.S.Database.ErrorCode;
using B.S.Database.Interface.Interface;
using B.S.Database.Write.Api.Application.Commands;
using MD5Hash;
using MediatR;

namespace B.S.Database.Write.Api.Application.CommandHandler
{
    public class LoginCreateCommandHandler : IRequestHandler<LoginCreateCommands, Apiresult<object>>
    {
        private readonly IBaseResotity<User> userReposity;
        private readonly IBaseResotity<UserRole> userRoleReosity;
        private readonly IBaseResotity<Role> roleResotity;
        private readonly IBaseResotity<Promission> promissionRepostity;
        private readonly IBaseResotity<RolePromission> rolePromissionResotity;
        private readonly IMapper mapper;

        public LoginCreateCommandHandler(IBaseResotity<User> UserReposity,IBaseResotity<UserRole> UserRoleReosity,IBaseResotity<Role> RoleResotity,IBaseResotity<Promission> PromissionRepostity,IBaseResotity<RolePromission> RolePromissionResotity,IMapper mapper)
        {
            userReposity = UserReposity;
            userRoleReosity = UserRoleReosity;
            roleResotity = RoleResotity;
            promissionRepostity = PromissionRepostity;
            rolePromissionResotity = RolePromissionResotity;
            this.mapper = mapper;
        }
        public Task<Apiresult<object>> Handle(LoginCreateCommands request, CancellationToken cancellationToken)
        {
            Apiresult<object> res = new Apiresult<object>();
            res.Code = ApiEnums.成功;
            res.Message = ApiEnums.成功.ToString();
            User user = mapper.Map<User>(request);
            user.UserName = request.UserName;
            user.UserNickName = request.UserNickName;
            user.UserPass = request.UserPass.GetMD5();
            userReposity.Add(user);
            UserRole userRole = mapper.Map<UserRole>(request);
            userRole.RoleId = request.RoleId;
            userRole.UserId = user.UserId;
            userRoleReosity.Add(userRole);
            res.Code = ApiEnums.成功;
            res.Message = ApiEnums.成功.ToString();
            return Task.FromResult(res);
        }
    }
}
